//
//  PrefsV2UnitsViewController.m
//  Fuel Log 4
//
//  Created by Olaf on 22.05.10.
//  Copyright 2010 olafgreck. All rights reserved.
//

#import "PrefsV2NumericSubViewController.h"
#import "PrefsV2ViewController.h" 
#import "FuelLogAppDelegate.h"
#import "Pref.h"

#import "DebugOutput.h"

@implementation PrefsNumericSubViewController

@synthesize  preferencesPrimaryArray;
@synthesize preferencesSecondaryArray;
@synthesize preferencesTitleArray;
@synthesize indicatorForPreferenceToWorkOn;
@synthesize numericToWorkOn;
@synthesize doneButton;

#pragma mark -
#pragma mark Initialization

- (IBAction)done:(id)sender {
	Pref *pref = [FuelLogAppDelegate pref];
	switch (indicatorForPreferenceToWorkOn) {
		case prefRow_Units:
				[pref setUnits:numericToWorkOn];
			break;
		case prefRow_DistanceDigits:
			[pref setOdometerDigits:numericToWorkOn];
			break;
		case prefRow_VolumeDigits:
			[pref setVolumeDigits:numericToWorkOn+2];
			break;
		case prefRow_CostDigits:
			[pref setCostDigits:numericToWorkOn+2];
			break;
		case statsRow_ObservationPeriod:
//				<#statements#>
				break;

		default:
			break;
	}

	[self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
		doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
					  //style:UIBarButtonItemStylePlain
																   target:self
																   action:@selector(done:)];
		self.navigationItem.rightBarButtonItem = doneButton;
#warning update and set title from caling navController
		self.navigationItem.title = (NSLocalizedString(@"Preferences", @"Preferences"));
    }

 	return self;
}



#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

 
- (void)viewWillAppear:(BOOL)animated {
	switch (indicatorForPreferenceToWorkOn) {
		case prefRow_Units:
			self.numericToWorkOn = [FuelLogAppDelegate pref].units;
			break;
		case prefRow_DistanceDigits:
			self.numericToWorkOn = [FuelLogAppDelegate pref].odometerDigits;
			break;
		case prefRow_VolumeDigits:
			self.numericToWorkOn = [FuelLogAppDelegate pref].volumeDigits-2;
			break;
		case prefRow_CostDigits:
			self.numericToWorkOn = [FuelLogAppDelegate pref].costDigits-2;
			break;
		case statsRow_ObservationPeriod:
			self.numericToWorkOn = 99;
		default:
			break;
	}
 if (numericToWorkOn <0) numericToWorkOn = 0;
	
		 
    [super viewWillAppear:animated];
}
 
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
	
	return  preferencesTitleArray; 
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [preferencesPrimaryArray count];
}


// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSInteger row = indexPath.row;
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
	}
	cell.accessoryType=UITableViewCellAccessoryNone;
	cell.textLabel.text = [preferencesPrimaryArray objectAtIndex:row] ;
	cell. detailTextLabel.text =  [preferencesSecondaryArray objectAtIndex:row];
	if (row == numericToWorkOn) cell.accessoryType= UITableViewCellAccessoryCheckmark; 
	return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	self.numericToWorkOn = (NSInteger ) indexPath.row;
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	[tableView reloadData];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[preferencesPrimaryArray release];
	[preferencesSecondaryArray release];
	[doneButton release];
    [super dealloc];
}


@end

